﻿Imports System.Web

'created by : sum 
'created date: 13/9/2011
'description: general function due with cookie :D
Namespace helper
    ''' <summary>
    ''' Provides methods due with cookie
    ''' </summary>
    ''' <remarks></remarks>
    Public NotInheritable Class CookieHelper


        ''' <summary>
        ''' Delete Cookie from client side
        ''' </summary>
        ''' <param name="name">Cookie Name</param>
        ''' <remarks></remarks>
        Public Shared Sub DeleteCookie(ByVal name As String)
            If String.IsNullOrEmpty(name) Then
                Throw New ArgumentNullException("name")
            End If
            Dim cookie As New HttpCookie(name) With { _
              .Expires = New DateTime(1999, 1, 1) _
            }
            HttpContext.Current.Response.Cookies.Add(cookie)

        End Sub



        ''' <summary>
        ''' Get Value From Cookie
        ''' </summary>
        ''' <param name="name">cookie name</param>
        ''' <param name="defaultStr">default value, if cookie does not exist will return this value</param>
        ''' <returns>if cookie exist return cookie value,otherwise return defaultStr </returns>
        ''' <remarks></remarks>
        Public Shared Function GetStringFromCookie(ByVal name As String, ByVal defaultStr As String) As String
            If String.IsNullOrEmpty(name) Then
                Throw New ArgumentNullException("name")
            End If
            Dim cookie As HttpCookie = HttpContext.Current.Request.Cookies(name)
            If (cookie IsNot Nothing) AndAlso (cookie.Value IsNot Nothing) Then
                Return cookie.Value
            End If
            Return defaultStr
        End Function

        ''' <summary>
        ''' delete the cookie if value is equal to deletevalue
        ''' </summary>
        ''' <param name="CookieName">Cookie Name</param>
        ''' <param name="deleteValue">delete value</param>
        ''' <returns>if cookie value is equal to delete value return nothing and delete the cookie, otherwise return the value</returns>
        ''' <remarks></remarks>
        Public Shared Function GetStringFromCookieAndDelete(ByVal CookieName As String, ByVal deleteValue As String) As String
            If String.IsNullOrEmpty(CookieName) Then
                Throw New ArgumentNullException("CookieName")
            End If
            If String.IsNullOrEmpty(deleteValue) Then
                Throw New ArgumentNullException("deleteValue")
            End If
            Dim stringFromCookie As String = GetStringFromCookie(CookieName, Nothing)
            If stringFromCookie = deleteValue Then
                DeleteCookie(CookieName)
                Return Nothing
            End If
            Return stringFromCookie
        End Function

        ''' <summary>
        ''' create a cookie at client side
        ''' </summary>
        ''' <param name="CookieName">Cookie Name</param>
        ''' <param name="CookieValue">Cookie Value</param>
        ''' <param name="expires">Cookie expiry date</param>
        ''' <remarks></remarks>
        Public Shared Sub WriteCookie(ByVal CookieName As String, ByVal CookieValue As String, ByVal expires As System.Nullable(Of DateTime))
            If String.IsNullOrEmpty(CookieName) Then
                Throw New ArgumentNullException("name")
            End If
            Dim cookie As New HttpCookie(CookieName, CookieValue)
            If expires.HasValue Then
                cookie.Expires = expires.Value
            End If
            HttpContext.Current.Response.Cookies.Add(cookie)
        End Sub

        ''' <summary>
        ''' Create a persistent cookie
        ''' </summary>
        ''' <param name="name">cookie name</param>
        ''' <param name="str">cookie value</param>
        ''' <remarks></remarks>
        Public Shared Sub WritePersistentCookie(ByVal name As String, ByVal str As String)
            If String.IsNullOrEmpty(name) Then
                Throw New ArgumentNullException("name")
            End If
            WriteCookie(name, str, New System.Nullable(Of DateTime)(DateTime.Now.AddYears(50)))
        End Sub
    End Class
End Namespace

